package sw6.pc.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import org.xml.sax.SAXException;

import sw6.pc.logic.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AdministrationForm extends JFrame {

	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_2;
	private Menu mActiveMenu;
	private JScrollBar scrollBar;
	private JPanel panel_1;
	private int mLastScrollValue = 0;
	private JButton btnReturn;
	private Stack<Menu> mMenuStack = new Stack<Menu>();

	/**
	 * Create the frame.
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public AdministrationForm() throws SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 747, 641);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		scrollBar = new JScrollBar();
		scrollBar.addAdjustmentListener(new AdjustmentListener() {
			public void adjustmentValueChanged(AdjustmentEvent arg0) {
					mActiveMenu.moveMenu(mLastScrollValue - arg0.getValue());
					mLastScrollValue = scrollBar.getValue();
			}
		});
		scrollBar.setBounds(338, 48, 17, 546);
		contentPane.add(scrollBar);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3, true));
		panel_1.setBounds(10, 48, 328, 546);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		panel = new JPanel();
		panel.setBounds(3, 3, 320, 540);
		panel_1.add(panel);
		panel.setBackground(Color.WHITE);
		panel.setBorder(null);
		panel.setLayout(null);
		
		btnReturn = new JButton("Tilbage");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Back();
			}
		});
		btnReturn.setBounds(249, 14, 89, 23);
		contentPane.add(btnReturn);
		
		panel_2 = new JPanel();
		panel_2.setBackground(Color.WHITE);
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 3));
		panel_2.setBounds(355, 48, 360, 544);
		contentPane.add(panel_2);
		
		//JScrollPane jsp = new JScrollPane(panel);
		//jsp.setBounds(10, 11, 323, 538);
		//jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		//contentPane.add(textArea);
		
		Menu menu = SettingsParser.LoadXML("Test.xml");
		
		GuiBuilder.BuildMenu(menu, "testJar");
		
		LoadMenu(menu);
	}
	
	private void Back() {
		// If the size of mMenuStack is bellow 2, we are at root menu, and should not allow to go back
		if (mMenuStack.size() < 2)
			return;
		mMenuStack.pop();
		// Loads the previus menu.
		LoadMenu(mMenuStack.pop());
	}
	
	private void LoadMenu(final Menu menu) {
		panel.removeAll();
		
		mActiveMenu = menu;
		
		// This hooks up an ActionListener on auto-generated JButtons, used to enter sub-menues.
		menu.setActionListner(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// If something is pressed we want to know if it was a button
				if (arg0.getSource().getClass() != JButton.class)
					return;
				
				// Loops through all JComponents in the active menu, looking for JButtons
				for (int i = 0; i < mActiveMenu.getJComponents().size(); i++) {
					if (mActiveMenu.getJComponents().get(i).getClass() == JButton.class) {
						JButton button = (JButton)mActiveMenu.getJComponents().get(i);
						
						if (button.getText() == ((JButton)arg0.getSource()).getText()) {

							for (int j = 0; j < mActiveMenu.getMenus().size(); j++) {
								// If the text of the JButton is equal to the text of some sub-menu, it must be a sub-menu JButton
								if (mActiveMenu.getMenus().get(j).getRealName() == button.getText()) {
									// Loads the sub-menu
									LoadMenu(mActiveMenu.getMenus().get(j));
								}
							}
						}
					}
				}
			}
		});
		
		int counter = 0;
		for (JComponent component : menu.getJComponents()) {
			if (component.getClass() == JPanel.class){
				final int finalCounter = counter;
				for (Component jPanelComponent : ((JPanel)component).getComponents()) {
					
					
					if (jPanelComponent.getClass() == JButton.class) {
						final JButton button = (JButton)jPanelComponent;
						
						button.addActionListener(new ActionListener() { 
							public void actionPerformed(ActionEvent arg0) {
								if (button.getText() == "Rediger") {
									Setting setting = menu.getSettings().get(finalCounter);
									loadSetting(setting);
								}
							}
						});
							
					}
				}
				counter++;
			}

		}
		
		for (int i = 0; i < menu.getJComponents().size(); i++) {
			panel.add(menu.getJComponents().get(i));
		}
		
		if (menu.getMenuHeight() - panel.getHeight() < 1)
			scrollBar.disable();
		else{
			scrollBar.enable();
			scrollBar.setMaximum(menu.getMenuHeight() - panel.getHeight());
			mLastScrollValue = scrollBar.getValue();
		}
		
		mMenuStack.add(menu);
		repaint();
		panel.repaint();
	}
	
	private void loadSetting(Setting setting) {
		panel_2.add(setting.getJPanel());
	}
}